package com.zhentao.controller;

import com.github.pagehelper.PageInfo;
import com.zhentao.model.dto.system.SysRoleDto;
import com.zhentao.model.entity.system.SysRole;
import com.zhentao.model.vo.common.Result;
import com.zhentao.model.vo.common.ResultCodeEnum;
import com.zhentao.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping(value = "/system/sysRole")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    //分页查询所有角色
    @PostMapping(value = "/findByPage/{pageNum}/{pageSize}")
    public Result<PageInfo<SysRole>> findByPage(@RequestBody SysRoleDto sysRoleDto,
                                                @PathVariable(value = "pageNum") Integer pageNum,
                                                @PathVariable(value = "pageSize") Integer pageSize){
        PageInfo<SysRole> pageInfo = sysRoleService.findByPage(sysRoleDto,pageNum,pageSize);
        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }

    //TODO:添加角色
    @PostMapping(value = "/saveSysRole")
    public Result add(@RequestBody SysRole sysRole){
        sysRoleService.saveSysRole(sysRole);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    //TODO:修改角色
    @PutMapping(value = "/updateSysRole")
    public Result update(@RequestBody SysRole sysRole){
        sysRoleService.updateSysRole(sysRole);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    //TODO:删除角色
    @DeleteMapping(value = "/deleteById/{roleId}")
    public Result delete(@PathVariable(value = "roleId") Long roleId){
        sysRoleService.deleteById(roleId);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }


    //TODO:给用户分配角色
    @GetMapping(value = "/findAllRoles/{userId}")
    public Result<Map<String, Object>> findAllRoles(@PathVariable(value = "userId") Long userId){
        Map<String, Object> resultMap = sysRoleService.findAllRoles(userId);
        return Result.build(resultMap,ResultCodeEnum.SUCCESS);
    }
}
